# Create your views here.
from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response
from django.template import RequestContext

from models import Zone, ZoneArray

def zone_impacts(request, pk):
    try:
        z = Zone.objects.get(pk=pk)
    except:
        return HttpResponse("No zone with that pk", status=404)
    zoned = [x for x in z.impacts if x.zoned]
    original = [x for x in z.impacts if x.raster.name == 'original']
    scenarios = [x for x in z.impacts if not x.zoned and x.raster.name != 'original']
    return render_to_response('zones/impacts.html', {'zoned': zoned, 'original': original, 'scenarios': scenarios}, context_instance=RequestContext(request))

def group_impacts(request, pk):
    try:
        g = ZoneArray.objects.get(pk=pk)
    except:
        return HttpResponse("No zone group with that pk", status=404)
    if not g.impacts:
        return HttpResponse("Still waiting for processing to complete...")
    zoned = [x for x in g.impacts if x['rast'] == 'zoned']
    original = [x for x in g.impacts if x['rast'] == 'original']
    scenarios = [x for x in g.impacts if x['rast'] not in ['original','zoned'] ]
    return render_to_response('zones/group_impacts.html', {'zoned': zoned, 'original': original, 'scenarios': scenarios}, context_instance=RequestContext(request))

def zone_threats(request, pk):
    try:
        z = Zone.objects.get(pk=pk)
    except:
        return HttpResponse("No zone with that pk", status=404)
    impacts = z.threats
    sum_avg_threat = 0
    all_done = True
    for i in impacts:
        if not i.pixels:
            all_done = False
            sum_avg_threat = None
            break
        sum_avg_threat += i.avg
    return render_to_response('zones/activities.html', {'impacts': impacts, 'sum_avg_threat': sum_avg_threat}, context_instance=RequestContext(request))
